package com.jason.www.widget;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.WindowInsets;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

/**
 * @author：Jason
 * @date：2021/7/21 15:41
 * @email：1129847330@qq.com
 * @description:
 */
public class WindowInsetsLinearLayout extends LinearLayout {

    public WindowInsetsLinearLayout(Context context) {
        this(context, null);
    }

    public WindowInsetsLinearLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WindowInsetsLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOnHierarchyChangeListener(new OnHierarchyChangeListener() {
            @Override
            public void onChildViewAdded(View parent, View child) {
                requestApplyInsets();
            }

            @Override
            public void onChildViewRemoved(View parent, View child) {
            }
        });
    }

    @TargetApi(Build.VERSION_CODES.KITKAT_WATCH)
    @Override
    public WindowInsets onApplyWindowInsets(WindowInsets insets) {
        int childCount = getChildCount();
        for (int index = 0; index < childCount; index++) {
            getChildAt(index).dispatchApplyWindowInsets(insets);
        }
        return insets;
    }

    //@Override
    //public WindowInsets dispatchApplyWindowInsets(WindowInsets insets) {
    //    WindowInsets result = super.dispatchApplyWindowInsets(insets);
    //    if (!insets.isConsumed()) {
    //        final int count = getChildCount();
    //        for (int i = 0; i < count; i++)
    //            result = getChildAt(i).dispatchApplyWindowInsets(insets);
    //    }
    //    return result;
    //}
    //
    //@Override
    //public void addView(View child, int index, ViewGroup.LayoutParams params) {
    //    super.addView(child, index, params);
    //    ViewCompat.requestApplyInsets(child);
    //}
}